package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageInfo;
import com.macro.mall.mapper.SapAccountMapper;
import com.macro.mall.mapper.SapAccountVertifyRecordMapper;
import com.macro.mall.model.SapAccount;
import com.macro.mall.model.SapAccountExample;
import com.macro.mall.model.SapAccountVertifyRecord;
import com.macro.mall.model.SapAccountVertifyRecordExample;
import com.macro.mall.portal.service.SapAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class SapAccountServiceImpl implements SapAccountService {

    @Autowired
    private SapAccountMapper sapAccountMapper;
    @Autowired
    private SapAccountVertifyRecordMapper sapAccountVertifyRecordMapper;

    public PageInfo<SapAccount> getSapAccountList(Long memberId) {
        SapAccountExample sapAccountExample = new SapAccountExample();
        SapAccountExample.Criteria criteria = sapAccountExample.createCriteria();
        criteria.andMemberIdEqualTo(memberId);
        List<SapAccount> sapAccounts = sapAccountMapper.selectByExample(sapAccountExample);
        PageInfo<SapAccount> pageInfo = new PageInfo<>(sapAccounts);
        return pageInfo;
    }

    @Override
    public void extractSapAccount(Long memberId, String balance, String type) {
        SapAccountExample sapAccountExample = new SapAccountExample();
        SapAccountExample.Criteria criteria = sapAccountExample.createCriteria();
        criteria.andMemberIdEqualTo(memberId);
        List<SapAccount> sapAccounts = sapAccountMapper.selectByExample(sapAccountExample);
        if (sapAccounts != null) {
            SapAccount sapAccount = sapAccounts.get(0);
            SapAccountVertifyRecord sapAccountVertifyRecord = new SapAccountVertifyRecord();
            sapAccountVertifyRecord.setStatus("0");
            sapAccountVertifyRecord.setAccountId(sapAccount.getId());
            sapAccountVertifyRecord.setType(type);
            sapAccountVertifyRecord.setAccountBalance(new BigDecimal(balance));
            sapAccountVertifyRecord.setCreateTime(new Date());
            sapAccountVertifyRecordMapper.insert(sapAccountVertifyRecord);
        }
    }

    @Override
    public void vertifyApply(Long memberId, String status) {

        SapAccountVertifyRecord sapAccountVertifyRecord = null;
        SapAccount sapAccount = null;

        SapAccountExample sapAccountExample = new SapAccountExample();
        SapAccountExample.Criteria criteria = sapAccountExample.createCriteria();
        criteria.andMemberIdEqualTo(memberId);
        List<SapAccount> sapAccounts = sapAccountMapper.selectByExample(sapAccountExample);
        if (sapAccounts != null) {
            sapAccount = sapAccounts.get(0);
            SapAccountVertifyRecordExample sapAccountVertifyRecordExample = new SapAccountVertifyRecordExample();
            SapAccountVertifyRecordExample.Criteria criteria1 = sapAccountVertifyRecordExample.createCriteria();
            criteria1.andAccountIdEqualTo(sapAccount.getId());
            List<SapAccountVertifyRecord> sapAccountVertifyRecords = sapAccountVertifyRecordMapper.selectByExample(sapAccountVertifyRecordExample);
            if (null != sapAccountVertifyRecords) {
                sapAccountVertifyRecord = sapAccountVertifyRecords.get(0);
                if (status.equals("1")) {
                    BigDecimal subtract = sapAccount.getAccountBalance().subtract(sapAccountVertifyRecord.getAccountBalance());
                    sapAccount.setMemberId(memberId);
                    sapAccount.setAccountBalance(subtract);
                    sapAccountMapper.updateByPrimaryKeySelective(sapAccount);
                    sapAccountVertifyRecordMapper.deleteByPrimaryKey(sapAccountVertifyRecord.getId());
                }
            } else {
                new RuntimeException("申请信息有误");
            }
        } else {
            new RuntimeException("账户信息有误");
        }

    }
}
